package com.ruoyi.basebill.task;


import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.service.IOmsDeliverbillService;
import com.ruoyi.basebill.service.IOmsStocktransferbillService;
import com.ruoyi.basebill.task.util.OmsTaskUtil;
import com.ruoyi.basedata.domain.BdWarehouse;
import com.ruoyi.basedata.service.IBdWarehouseService;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 定时读取发货确认单，生成下游单据
 */
@Component("OmsDeliverbillTask")
public class OmsDeliverbillTask {

    @Autowired
    private IOmsDeliverbillService omsDeliverbillService;

    @Autowired
    private IBdWarehouseService bdWarehouseService ;

    @Autowired
    private IOmsStocktransferbillService omsStocktransferbillService;

    @Autowired
    private OmsTaskUtil omsTaskUtil;

    @Autowired
    private OmsDeliverbillTask omsDeliverbillTask;


    public void pushDown(){
        OmsDeliverbill omsDeliverbill = new OmsDeliverbill();
        //查询iscreatebill不为1的数据
        omsDeliverbill.setIscreatebill(0L);
        List<OmsDeliverbill> omsDeliverbills = omsDeliverbillService.selectOmsDeliverbillList(omsDeliverbill);
        omsDeliverbill.setIscreatebill(-1L);
        List<OmsDeliverbill> omsDeliverbills1 = omsDeliverbillService.selectOmsDeliverbillList(omsDeliverbill);
        omsDeliverbills.addAll(omsDeliverbills1);
        for(OmsDeliverbill omsDelbill : omsDeliverbills){
            omsDelbill = omsDeliverbillService.selectOmsDeliverbillById(omsDelbill.getId());
            OmsDeliverbill omsDelbill1 = omsDeliverbillTask.pushDownMethod(omsDelbill);
            omsDeliverbillService.updateOmsDeliverbill(omsDelbill1);
        }
    }
    @Transactional
    public OmsDeliverbill pushDownMethod(OmsDeliverbill omsDelbill){
        try{
            BdWarehouse bdWarehouse = new BdWarehouse();
            bdWarehouse.setWarehousenumber(omsDelbill.getFacilitycode());
            //根据单头facilityCode字段查询仓库基础资料，如果仓库的“在途仓编码”字段为空
            // 则直接将isCreateBill字段置为1，reason置为“无在途仓”；
            List<BdWarehouse> bdWarehouses =
                    bdWarehouseService.selectBdWarehouseList(bdWarehouse);
            if(bdWarehouses==null||bdWarehouses.size()==0){
                omsDelbill.setIscreatebill(-1L);
                omsDelbill.setReason("仓库为空");
                return omsDelbill;
            }
            if(StringUtils.isBlank(bdWarehouses.get(0).getTransferwhnumber())){
                omsDelbill.setIscreatebill(-2L);
                omsDelbill.setReason(bdWarehouses.get(0).getWarehousenumber() + "无在途仓");
                return omsDelbill;
            }
            else{
                //如果仓库的“在途仓编码”字段不为空则生成一张库存调拨单。生成成功后回写isCreateBill为1。
                OmsStocktransferbill omsStocktransferbill = new OmsStocktransferbill();
                omsStocktransferbill.setBizdate(omsDelbill.getDeliverydate());
                omsStocktransferbill.setBillnumber(omsTaskUtil.setBillNumber(omsStocktransferbill));
                omsStocktransferbill.setBiztypenumber("331");
                omsStocktransferbill.setBiztypename("跨仓库调拨（成本价）");
                omsStocktransferbill.setIssuestorageorgnumber(omsDelbill.getInventoryorganizationcode());
                omsStocktransferbill.setIssuestorageorgname(omsDelbill.getInventoryorganizationname());
                omsStocktransferbill.setReceiptstorageorgnumber(omsDelbill.getInventoryorganizationcode());
                omsStocktransferbill.setReceiptstorageorgname(omsDelbill.getInventoryorganizationname());
                omsStocktransferbill.setCurrency(omsDelbill.getCurrency());
                omsStocktransferbill.setExchangerate(omsDelbill.getExchangerate());
                omsStocktransferbill.setSrcbilltype("发货确认");
                omsStocktransferbill.setSrcbillnumber(omsDelbill.getCode());
                omsStocktransferbill.setCreatetime(new Date());
                omsStocktransferbill.setSrcsys(omsDelbill.getSrcsys());
                List<OmsStocktransferbillentry> omsStocktransferbillentry = new ArrayList();
                if(omsDelbill.getOmsDeliverbillentryList()!=null){
                    for (OmsDeliverbillentry delbillentry:omsDelbill.getOmsDeliverbillentryList()){
                        OmsStocktransferbillentry stocktransferbillentry = new OmsStocktransferbillentry();
                        stocktransferbillentry.setMaterialnumber(delbillentry.getSkucode());
                        stocktransferbillentry.setMaterialname(delbillentry.getSkuname());
                        stocktransferbillentry.setIssuewarehousenumber(omsDelbill.getFacilitycode());
                        stocktransferbillentry.setIssuewarehousename(omsDelbill.getFacilityname());
                        stocktransferbillentry.setReceiptwarehousenumber(bdWarehouses.get(0).getTransferwhnumber());
                        stocktransferbillentry.setReceiptwarehousename(bdWarehouses.get(0).getTransferwhname());
                        stocktransferbillentry.setUnitnumber(delbillentry.getUnit());
                        stocktransferbillentry.setBaseunitnumber(delbillentry.getBaseunit());
                        stocktransferbillentry.setQty(delbillentry.getShipqty());
                        stocktransferbillentry.setBaseqty(delbillentry.getBaseshipqty());
                        stocktransferbillentry.setIssueplandate(omsDelbill.getDeliverydate());
                        stocktransferbillentry.setReceiptplandate(omsDelbill.getDeliverydate());
                        stocktransferbillentry.setIspresent(delbillentry.getIsgift());
                        omsStocktransferbillentry.add(stocktransferbillentry);
                    }
                }
                omsStocktransferbill.setOmsStocktransferbillentryList(omsStocktransferbillentry);
                omsStocktransferbillService.insertOmsStocktransferbill(omsStocktransferbill);
                omsDelbill.setIscreatebill(1L);
                omsDelbill.setReason("");
                omsDeliverbillService.updateOmsDeliverbill(omsDelbill);
            }
        }catch (Exception e){
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            omsDelbill.setReason(e.getMessage());
            omsDelbill.setIscreatebill(-1L);
            return omsDelbill;
        }
        return omsDelbill;
    }
}
